package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.GoodType;
import org.millioncall.yueyoga.admin.service.GoodTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lisj Mar 27, 2014 2:50:21 PM
 */
@Controller
public class GoodTypeAction extends BaseAction {

	private GoodTypeService goodTypeService;

	public GoodTypeService getGoodTypeService() {
		return goodTypeService;
	}

	public void setGoodTypeService(GoodTypeService goodTypeService) {
		this.goodTypeService = goodTypeService;
	}

	@RequestMapping(value = "/toAddGoodType.jhtml")
	public String toAddGoods(ModelMap model,HttpServletRequest request,HttpServletResponse response) {
		super.initFrontHeader(request, model);
		return "goodtype_add";
	}
	
	@RequestMapping(value = "/addGoodType.jhtml")
	public String addGoods(ModelMap context,HttpServletRequest request,HttpServletResponse response) {
		GoodType goodType=new GoodType();
		goodType.setEnable(true);
		goodType.setName(request.getParameter("name"));
		goodType.setRemarks(request.getParameter("remarks"));
		if (goodTypeService.save(goodType)) {
			context.put("msg","添加商品类型成功!");
			return "redirect:findGoodType.jhtml";
		}else{
			context.put("msg","添加商品类型失败,请联系系统管理员!");
			return "redirect:toAddGoodType.jhtml";
		}
	}
	
	// 查询商品信息
	@RequestMapping(value = "/findGoodType.jhtml")
	public String findGoodsType(String name, String remarks, HttpServletRequest request, ModelMap model) {
		super.initFrontHeader(request, model);
		int pageNo = 1;
		int pageSize = 10;
		String strStart = request.getParameter("pageNo");// 获得页码数
		if (StringUtils.isNotBlank(strStart)) {
			pageNo = Integer.parseInt(strStart);
		}
		String strLimit = request.getParameter("limit");
		if (strLimit != null) {
			pageSize = Integer.parseInt(strLimit);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(name)){
			map.put("name","%"+name+"%");
			model.put("name", name);
		}
		PaginatedList<GoodType> GoodTypes = goodTypeService.findGoodTypeInPage(map, (pageNo - 1) * 10, pageSize);
		model.put("goodTypes", GoodTypes);
		return "goodtype_search";
	}
	
	// 根据商品id集合删除商品信息
	@RequestMapping(value = "/deleteGoodType.jhtml")
	public void deleteGoodsByIds(String ids, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = "";
		if (StringUtils.isBlank(ids)) {
			msg = "false";
		} else {
			if (goodTypeService.deleteByIds(ids)) {
				msg = "true";
			} else {
				msg = "false";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
}
